﻿using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

namespace TCG
{
    /// <summary>
    /// 材质中使用的贴图属性
    /// </summary>
    [ProtoBuf.ProtoContract]
    public class TextureInMat
    {
        public TextureInMat () {}

        public TextureInMat (string propName, string texName, Vector2 offset, Vector2 scale)
        {
            propertyName = propName;
			textureId = CommonTools.ParseAssetIdFromName (texName);
            textureOffsetX = offset.x;
            textureOffsetY = offset.y;
            textureScaleX = scale.x;
            textureScaleY = scale.y;
        }

        [ProtoBuf.ProtoMember (1)]
        public string propertyName { get; set; }

		[ProtoBuf.ProtoMember (2)]
		public uint textureId { get; set; }

        [ProtoBuf.ProtoMember (3)]
        public float textureOffsetX { get; set; }

        [ProtoBuf.ProtoMember (4)]
        public float textureOffsetY { get; set; }

        [ProtoBuf.ProtoMember (5)]
        public float textureScaleX { get; set; }

        [ProtoBuf.ProtoMember (6)]
        public float textureScaleY { get; set; }

		public GameAsset<Texture> textureAsset { get; set; }

		public void Unload ()
		{
			GameAssetsDatabase.instance.textureDatabase.ReleaseAsset (textureAsset);
		}

        public override string ToString()
        {
            return string.Format(
                "[TextureInMat: propertyName={0}, textureId={1}, textureOffset=({2}, {3}, {4}, {5})]",
				propertyName, textureId, textureOffsetX, textureOffsetY, textureScaleX, textureScaleY);
        }
    }

    /// <summary>
    /// 材质中使用的Float属性
    /// </summary>
    [ProtoBuf.ProtoContract]
    public class FloatInMat
    {
        public FloatInMat () {}

        public FloatInMat (string propName, float value)
        {
            propertyName = propName;
            floatValue = value;
        }

        [ProtoBuf.ProtoMember (1)]
        public string propertyName { get; set; }

        [ProtoBuf.ProtoMember (2)]
        public float floatValue { get; set; }

        public override string ToString()
        {
            return string.Format("[FloatInMat: propertyName={0}, floatValue={1}]", 
                propertyName, floatValue);
        }
    }

    /// <summary>
    /// 材质中使用的颜色属性
    /// </summary>
    [ProtoBuf.ProtoContract]
    public class ColorInMat 
    {
        public ColorInMat () {}

        public ColorInMat (string propName, Color color)
        {
			propertyName = propName;
            a = color.a;
            r = color.r;
            g = color.g;
            b = color.b;
        }

        [ProtoBuf.ProtoMember (1)]
        public string propertyName { get; set; }

        [ProtoBuf.ProtoMember (2)]
        public float a { get; set; }

        [ProtoBuf.ProtoMember (3)]
        public float r { get; set; }

        [ProtoBuf.ProtoMember (4)]
        public float g { get; set; }

        [ProtoBuf.ProtoMember (5)]
        public float b { get; set; }

        public override string ToString()
        {
            return string.Format("[ColorInMat: propertyName={0}, color=({1}, {2}, {3}, {4})]", 
                propertyName, a, r, g, b);
        }
    }

    /// <summary>
    /// 材质中使用的Vector属性
    /// </summary>
    [ProtoBuf.ProtoContract]
    public class VectorInMat
    {
        public VectorInMat () {}

        public VectorInMat (string propName, Vector4 vector)
        {
            propertyName = propName;
            x = vector.x;
            y = vector.y;
            z = vector.z;
            w = vector.w;
        }

        [ProtoBuf.ProtoMember (1)]
        public string propertyName { get; set; }

        [ProtoBuf.ProtoMember (2)]
        public float x { get; set; }

        [ProtoBuf.ProtoMember (3)]
        public float y { get; set; }

        [ProtoBuf.ProtoMember (4)]
        public float z { get; set; }

        [ProtoBuf.ProtoMember (5)]
        public float w { get; set; }

        public override string ToString()
        {
            return string.Format("[VectorInMat: propertyName={0}, vec=({1}, {2}, {3}, {4})]", 
                propertyName, x, y, z, w);
        }
    }

    /// <summary>
    /// 可序列化的材质数据
    /// </summary>
    [ProtoBuf.ProtoContract]
    public class MaterialData
    {
        [ProtoBuf.ProtoMember (1)]
        public string shaderName { get; set; }

        [ProtoBuf.ProtoMember (2)]
        public IList<TextureInMat> textures { get; set; }

        [ProtoBuf.ProtoMember (3)]
        public IList<FloatInMat> floats { get; set; }

        [ProtoBuf.ProtoMember (4)]
        public IList<ColorInMat> colors { get; set; }

        [ProtoBuf.ProtoMember (5)]
        public IList<VectorInMat> vectors { get; set; }

		[ProtoBuf.ProtoMember (6)]
		public string matName { get; set;}

		public void Unload ()
		{
			if (textures != null)
			{
				for (int i = 0, len = textures.Count; i < len; ++ i)
				{
					textures[i].Unload ();
				}
			}
		}

        public override string ToString()
        {
            return string.Format(
				"[MaterialData: name={5} shaderName={0}, textures={1}, floats={2}, colors={3}, vectors={4}]", 
                shaderName, 
                CommonTools.JointCollection<TextureInMat> (textures), 
                CommonTools.JointCollection<FloatInMat> (floats), 
                CommonTools.JointCollection<ColorInMat> (colors), 
                CommonTools.JointCollection<VectorInMat> (vectors), 
				matName);
        }

        public void Serialize (System.IO.Stream stream)
        {
			ProtoBuf.Serializer.Serialize<MaterialData> (stream, this);
			Debug.Log (this.ToString ());
        }
    }
}